package org.mock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.mock.entity.SabreHotelPriceCheck;
import org.mock.enums.SabreMockStateEnum;
import org.mock.mapper.SabreHotelPriceCheckMapper;
import org.mock.service.SabreHotelPriceCheckService;
import org.mock.dto.SabreHotelPriceCheckDTO;
import org.mock.common.Condition;
import org.mock.service.SabreMockSceneService;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * 服务实现类
 *
 * @author AI
 * @since 2025-06-11
 */
@Service
@AllArgsConstructor
@Slf4j
public class SabreHotelPriceCheckServiceImpl implements SabreHotelPriceCheckService {

    protected SabreHotelPriceCheckMapper sabreHotelPriceCheckMapper;
    private SabreMockSceneService sabreMockSceneService;

    @Override
    public SabreHotelPriceCheckDTO handleRequest(JSONObject request) {
        log.info("[priceCheck] 请求：{}", request);
        SabreHotelPriceCheckDTO dto = new SabreHotelPriceCheckDTO();

        try {
            JSONObject requestJSONObject = request.getJSONObject("HotelPriceCheckRQ");
            JSONObject rateInfoRef = requestJSONObject.getJSONObject("RateInfoRef");
            String rateKey = rateInfoRef.get("RateKey").toString();
            dto.setRateKey(rateKey);
        } catch (Exception e) {
            log.error("请求解析异常：{}", e.getMessage(), e);
        }

        return dto;
    }

    @Override
    public SabreHotelPriceCheckDTO handleQueryWrapper(SabreHotelPriceCheckDTO dto) {
        String currentMockNo = sabreMockSceneService.getMockNoByStatus(SabreMockStateEnum.ON_REQUEST.getValue());

        if ("000".equals(currentMockNo)) {
            log.info("当前查询模式：忽略所有的查询条件，直接给一个新的dto");
            dto = new SabreHotelPriceCheckDTO();
        }
        return dto;
    }

    @Override
    public String handleData(SabreHotelPriceCheck data) {
        if (ObjectUtil.isEmpty(data)) {
            log.info("暂无数据");
            return StrUtil.EMPTY;
        }
        return data.getResponse();
    }

    @Override
    public SabreHotelPriceCheck getOne(SabreHotelPriceCheckDTO dto) {
        List<SabreHotelPriceCheck> list = sabreHotelPriceCheckMapper.selectList(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, SabreHotelPriceCheck.class)));
        if (CollUtil.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
//        return sabreHotelDetailMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto,SabreHotelDetail.class)));
    }
}
